/**
 * 
 */
package command;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.mad.code.transformation.ViewTransformation;
import org.mad.code.util.PropertiesManager;

import MobileApplicationDeveloper.model.AppMobile;
import MobileApplicationDeveloper.model.View;

/**
 * @author E420611
 * 
 */
public class NewProjectCommand {

	private AppMobile appParam;

	public void run() {
		// create a project with name "name" parameter
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		View mainView = appParam.getMainView();
		List<View> views = mainView.getNextViews();
		IProject project = root.getProject(appParam.getAppName());
		try {
			project.create(null);
			project.open(null);

			// set the Java nature
			IProjectDescription description = project.getDescription();
			description.setNatureIds(new String[] { JavaCore.NATURE_ID });

			// create the project
			project.setDescription(description, null);
			IJavaProject javaProject = JavaCore.create(project);

			
			// set the build path
			IClasspathEntry[] buildPath = {	
					JavaCore.newSourceEntry(project.getFullPath().append("src")),
					JavaRuntime.getDefaultJREContainerEntry() };

			javaProject.setRawClasspath(buildPath, project.getFullPath()
					.append("bin"), null);

			// create folder by using resources package
			IFolder folder = project.getFolder("src");
			folder.create(true, true, null);

			// Add folder to Java element
			IPackageFragmentRoot srcFolder = javaProject
					.getPackageFragmentRoot(folder);

			 
			 
			// create package fragment
			IPackageFragment fragment = srcFolder.createPackageFragment(
					"org.mad.code.activity", true, null);

			// init code string and create compilation unit
			String str = ViewTransformation.getTemplateAndroid(mainView).toString();
//					
//					"package com.programcreek;" + "\n"
//					+ "public class Test  {" + "\n" + " private String name;"
//					+ "\n" + "}";

			//this.createType(fragment, str, mainView.getViewName());
			
			
			for (View view : views) {
				this.createType(fragment, str, view.getViewName());
			}
			
//			try {
//				Runtime.getRuntime().exec("mvn clean install");
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			System.out.println(PropertiesManager.getProperty("jdbc.password"));
//			MavenCli cli = new MavenCli();
//			cli.doMain(new String[]{"clean install"}, "project_dir", System.out, System.out);
			
		} catch (CoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void createType(IPackageFragment fragment, String str, String name) {
		
		String className = name;
		ICompilationUnit cu;
		try {
			cu = fragment.createCompilationUnit(className + ".java",
					str, false, null);

			// create a field
			//IType type = cu.getType(className);
			IType fClass= cu.createType("package " + fragment.getElementName() + ";\n" + "public class " + className +" {\n}\n", null, true, null);			
			fClass.createField("private String name;", null, true, null);
			
		} catch (JavaModelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

	/**
	 * @return the appParam
	 */
	public AppMobile getAppParam() {
		return appParam;
	}

	/**
	 * @param appParam the appParam to set
	 */
	public void setAppParam(AppMobile appParam) {
		this.appParam = appParam;
	}


}
